import 'package:flutter/material.dart';
import 'package:flutter_vant/command/overLay.dart' as command;
import 'package:rlstyles/main.dart';

class VanNotifyType {
  static const String primary = 'primary';
  static const String success = 'success';
  static const String warning = 'warning';
  static const String danger = 'danger';
}

class VanNotifyOption {
  // 类型，可选值为 primary success warning
  final String? type;
  // 展示文案，支持通过\n换行
  final String? message;
  // 字体颜色
  final String? color;
  // 背景颜色
  final String? background;
  // 自定义时长
  final int? duration;
  // 自定义类名
  final Map<String, dynamic> styles;

  VanNotifyOption(
      {this.type = VanNotifyType.warning,
      this.message,
      this.color,
      this.background,
      this.duration = 500,
      this.styles = const {}});
}

class VanNotify {
  VanNotifyOption option;
  BuildContext context;

  static VanNotifyOption _option = VanNotifyOption();

  static getStyles() {
    return {
      'main': {CssRule.flexDirection: 'row'},
      'body': {
        CssRule.minHeight: 36,
        // CssRule.height:36,
        CssRule.width: '100%',
        CssRule.justifyContent: 'center',
        CssRule.alignItems: 'center',
        CssRule.color: _option.color ?? 'white',
        CssRule.paddingTop: 40,
        CssRule.paddingBottom: 10,
        ..._option.styles
      },
      'success': {
        CssRule.backgroundColor: '#07c160',
      },
      'warning': {
        CssRule.backgroundColor: '#ff976a',
      },
      'danger': {CssRule.backgroundColor: '#ee0a24'},
      'primary': {CssRule.backgroundColor: '#1989fa'},
      'default': {CssRule.backgroundColor: 'white', CssRule.color: 'black'}
    };
  }

  static renderNotify() {
    Map css = {
      '${_option.type}': true,
      'body': true,
    };
    final cls = StylesMap.getClass(css, getStyles());
    if (_option.background != null) {
      cls[CssRule.backgroundColor] = _option.background;
    }
    return Frame(
      styles: getStyles()['main'],
      children: [
        Frame(styles: cls, children: [TextView(_option.message ?? '')])
      ],
    );
  }

  VanNotify({required this.option, required this.context}) {
    _option = this.option;
    command.show(
        context: context,
        key: 'notify',
        child: renderNotify(),
        isShowTop: false);
    Future.delayed(Duration(milliseconds: 2000))
        .then((value) => command.remove());
  }
}
